<?php

namespace app\api\controller;

use app\api\logic\UserMessageLogic;
use app\basic\ApiController;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("站内信消息")
 */
class MessageController extends ApiController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new UserMessageLogic();
        parent::__construct();
    }

    /**
     * @Apidoc\Title("获取消息列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     * @Apidoc\Param("type", type="int", default="0", desc="消息类型：0=全部 1=系统消息 2=活动消息 3=通知消息")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *     @Apidoc\Returned("list", type="array", desc="消息列表", children={
     *         @Apidoc\Returned("id", type="int", desc="消息ID"),
     *         @Apidoc\Returned("type", type="int", desc="消息类型"),
     *         @Apidoc\Returned("title", type="string", desc="消息标题"),
     *         @Apidoc\Returned("content", type="string", desc="消息内容"),
     *         @Apidoc\Returned("status", type="int", desc="消息状态"),
     *         @Apidoc\Returned("create_time", type="string", desc="创建时间")
     *     }),
     *     @Apidoc\Returned("total", type="int", desc="总记录数"),
     *     @Apidoc\Returned("page", type="int", desc="当前页码"),
     *     @Apidoc\Returned("limit", type="int", desc="每页数量")
     * })
     */
    public function getList()
    {
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 10);
        $type = $this->request->post('type', 0);
        
        $list = $this->logic->getMessageList($this->auth->id, $type, $page, $limit);
        return $this->success($list);
    }

    /**
     * @Apidoc\Title("获取消息统计")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *     @Apidoc\Returned("unread_count", type="int", desc="未读消息数量"),
     *     @Apidoc\Returned("total_count", type="int", desc="总消息数量")
     * })
     */
    public function getCount()
    {
        $data = $this->logic->getMessageCount($this->auth->id);
        return $this->success($data);
    }

    /**
     * @Apidoc\Title("标记消息为已读")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("ids", type="string", require=true, desc="消息ID，多个以逗号分隔，如：1,2,3")
     */
    public function markAsRead()
    {
        $ids = $this->request->post('ids');
        if (empty($ids)) {
            return $this->fail('参数错误');
        }
        $result = $this->logic->markAsRead($this->auth->getUserId(), $ids);
        return $result ? $this->success('标记成功') : $this->fail('标记失败');
    }

    /**
     * @Apidoc\Title("标记所有消息为已读")
     * @Apidoc\Method("POST")
     */
    public function markAllAsRead()
    {
        $result = $this->logic->markAllAsRead($this->auth->id);
        return $result ? $this->success('标记成功') : $this->fail('标记失败');
    }

    /**
     * @Apidoc\Title("删除消息")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("ids", type="string", require=true, desc="消息ID，多个以逗号分隔，如：1,2,3")
     */
    public function delete()
    {
        $ids = $this->request->post('ids');
        if (empty($ids)) {
            return $this->fail('参数错误');
        }
        
        $result = $this->logic->deleteMessage($this->auth->id, $ids);
        return $result ? $this->success('删除成功') : $this->fail('删除失败');
    }
} 